<template>
  <a-modal
    :title='title'
    :width='840'
    :visible='visible'
    :confirmLoading='loading'
    @ok="() => { $emit('ok') }"
    @cancel="() => { $emit('cancel') }"
  >
    <a-spin :spinning='loading'>
      <a-form :form='form' v-bind='formLayout'>
        <a-row :gutter='24'>

          <a-col :span='12'>
            <a-form-item label='是否全部数据'>
              <a-switch v-model:checked="model.all"  checked-children="全部" un-checked-children="部分" />
            </a-form-item>
          </a-col>
          <a-col :span='12'>
            <a-form-item label='设备编码' v-if='!model.all'>
              <a-input v-model='model.equipmentNo' />
            </a-form-item>
          </a-col>
          <a-col :span='12'>
            <a-form-item label='版本号'>
              <a-input v-model='model.version' />
            </a-form-item>
          </a-col>
        </a-row>
      </a-form>

    </a-spin>
  </a-modal>
</template>

<script>
import pick from 'lodash.pick'
import { getDeviceGroupList } from '@/api/device'
// 表单字段
const fields = ['id', 'alarmsName', 'equipmentNo', 'password', 'alarmsGroup', 'address', 'ambientTemperature', 'chargingCapacity', 'chargingCurrent', 'chargingPower', 'chargingVoltage'
  , 'dischargeCurrent', 'dischargePower', 'dischargeVoltage', 'innerTemperature', 'innerTemperature', 'ledCurrent', 'ledPower', 'ledVoltage', 'streetlightVoltage']

export default {
  props: {
    title: {
      type: String,
      required: true
    },
    visible: {
      type: Boolean,
      required: true
    },
    loading: {
      type: Boolean,
      default: () => false
    },
    model: {
      type: Object,
      default: () => null
    }
  },
  watch: {
    model: {
      handler: function (val, oldVal) {
        console.log('model', val)
        this.model = val
      },
      deep: true
    }
  },
  data() {
    this.formLayout = {
      labelCol: {
        xs: { span: 24 },
        sm: { span: 7 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 13 }
      }
    }
    return {
      form: this.$form.createForm(this),
      groupList: []
    }
  },
  created() {
    // this.getGroupList()
    console.log('custom modal created')

    // 防止表单未注册
    fields.forEach(v => this.form.getFieldDecorator(v))

    // 当 model 发生改变时，为表单设置值
    // this.$watch('model', () => {
    //   this.model && this.form.setFieldsValue(pick(this.model, fields))
    //   this.model.alarmsData && this.form.setFieldsValue(pick(this.model.alarmsData, fields))
    // })
  },
  methods: {
    getGroupList() {
      var params = {
        pageNum: 1,
        pageSize: 100
      }
      getDeviceGroupList(params).then(res => {
        this.groupList = res.data.list
        console.log('groupList', this.groupList)
      })
    }
  }
}
</script>
